python學(xué)習(xí)日志str float int datetime格式轉(zhuǎn)換
發(fā)布時間:2024/11/19 23:31:23
====================================
數(shù)字轉(zhuǎn)換為字符
num = 123
char = str(num)
print(char) # 輸出: '123'
====================================
字符串轉(zhuǎn)換為float
s = "123.456"
f = float(s)
print(f)
====================================
字符轉(zhuǎn)數(shù)字
方法一:使用內(nèi)置的int()函數(shù)
number_str = "123"
number = int(number_str)
print(number)
方法二:使用eval()函數(shù)
number_str = "123"
number = eval(number_str)
print(number)
方法三:使用math庫中的math.floor()函數(shù)
import math
number_str = "123"
number = math.floor(float(number_str))
print(number)
======================================
字符轉(zhuǎn)日期
from datetime import datetime
date_str = '2023-04-01'
date_format = '%Y-%m-%d'
# 使用strptime轉(zhuǎn)換
date_obj = datetime.strptime(date_str, date_format)
print(date_obj) # 輸出: 2023-04-01 00:00:00
======================================
# ISO字符轉(zhuǎn)日期
date_object = isoparse("2024-11-19T23:24:15.6131472+08:00")
formatted_date=date_object.strftime("%Y-%m-%d %H:%M:%S")
轉(zhuǎn)換后結(jié)果:2024-11-19 23:24:15